package com.zhipu.balance.ui.vm.home.calculate.corn

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.SPUtils
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordCorn
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 玉米不完善粒
 */
class CornFaultinessViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 玉米不实体
     */
    var mRecordCorn : RecordCorn? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 不完善粒质量
     */
    var imperfectGrainQuality = ObservableField("")



    /**
     * 虫蚀粒质量
     */
    val insectBoredQuality = ObservableField("")

    /**
     * 病斑粒质量
     */
    val mottledQuality = ObservableField("")


    /**
     * 破损粒质量
     */
    var brokenQuality = ObservableField("")

    /**
     * 生芽粒重量
     */
    var sproutWeight = ObservableField("")

    /**
     * 生霉粒重量
     */
    var moldWeight = ObservableField("")

    /**
     * 热损伤粒质量
     */
    var thermalDamageGrainQuality = ObservableField("")
    /**
     * 霉变粒质量
     */
    var mildewGrainQuality = ObservableField("")

    /**
     * 小样重量
     */
    var smallMiscellaneousSampleQuality =  ObservableField("")

    /**
     * 大样杂质率
     */
    var detailResults =  ObservableField("")



    /**
     * 虫蚀率-结果
     */
    var insectBoredResult = ObservableField("")
    /**
     * 病斑率-结果
     */
    var mottledResult = ObservableField("")
    /**
     * 破损率-结果
     */
    var brokenResult = ObservableField("")
    /**
     * 生芽粒率-结果
     */
    var sproutResult = ObservableField("")
    /**
     * 生霉粒率-结果
     */
    var moldResult = ObservableField("")
    /**
     * 热损伤粒结果
     */
    var thermalDamageGrain = ObservableField("")
    /**
     * 霉变粒结果
     */
    var mildewGrain = ObservableField("")

    /**
     * 不完善粒-结果
     */
    var resultOfImperfectGrain = ObservableField("")







    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordCorn?.imperfectGrainRecorder)) {
            //不完善粒质量
            imperfectGrainQuality.set(mRecordCorn?.imperfectGrainQuality.toString())
            //虫蚀粒质量
            insectBoredQuality.set(mRecordCorn?.insectBoredQuality.toString())
            //病斑粒质量
            mottledQuality.set(mRecordCorn?.mottledQuality.toString())
            //破损粒质量
            brokenQuality.set(mRecordCorn?.brokenQuality.toString())
            //生芽粒重量
            sproutWeight.set(mRecordCorn?.sproutWeight.toString())
            //生霉粒重量
            moldWeight.set(mRecordCorn?.moldWeight.toString())
            //热损伤粒质量
            thermalDamageGrainQuality.set(mRecordCorn?.thermalDamageGrainQuality.toString())
            //霉变粒质量
            mildewGrainQuality.set(mRecordCorn?.mildewGrainQuality.toString())



            //小样重量
            smallMiscellaneousSampleQuality.set(mRecordCorn?.smallMiscellaneousSampleQuality.toString())
            //大样杂质率
            detailResults.set(mRecordCorn?.detailResults.toString())


            //虫蚀率-结果
            insectBoredResult.set(mRecordCorn?.insectBoredResult.toString())
            //病斑率-结果
            mottledResult.set(mRecordCorn?.mottledResult.toString())
            //破损率-结果
            brokenResult.set(mRecordCorn?.brokenResult.toString())
            //生芽粒率-结果
            sproutResult.set(mRecordCorn?.sproutResult.toString())
            //生霉粒率-结果
            moldResult.set(mRecordCorn?.moldResult.toString())
            //热损伤粒结果
            thermalDamageGrain.set(mRecordCorn?.thermalDamageGrain.toString())
            //霉变粒结果
            mildewGrain.set(mRecordCorn?.mildewGrain.toString())

            //不完善粒-结果
            resultOfImperfectGrain.set(mRecordCorn?.resultOfImperfectGrain.toString())

        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //不完善粒质量
        imperfectGrainQuality.set("")
        //虫蚀粒质量
        insectBoredQuality.set("")
        //病斑粒质量
        mottledQuality.set("")
        //破损粒质量
        brokenQuality.set("")
        //生芽粒重量
        sproutWeight.set("")
        //生霉粒重量
        moldWeight.set("")
        //热损伤粒质量
        thermalDamageGrainQuality.set("")
        //霉变粒质量
        mildewGrainQuality.set("")

        //小样重量
        smallMiscellaneousSampleQuality.set("")
        //大样杂质率
        detailResults.set("")



        //虫蚀率-结果
        insectBoredResult.set("")
        //病斑率-结果
        mottledResult.set("")
        //破损率-结果
        brokenResult.set("")
        //生芽粒率-结果
        sproutResult.set("")
        //生霉粒率-结果
        moldResult.set("")
        //热损伤粒结果
        thermalDamageGrain.set("")
        //霉变粒结果
        mildewGrain.set("")
        //不完善粒-结果
        resultOfImperfectGrain.set("")

    }




    /**
     * 不完善粒质量
     */
    fun imperfectGrainQuality(){
        imperfectGrainQuality.set(weighing)
        mRecordCorn?.imperfectGrainQuality = imperfectGrainQuality.get()?.toDouble()
    }

    /**
     * 虫蚀粒质量
     */
    fun insectBoredQuality(){
        insectBoredQuality.set(weighing)
        mRecordCorn?.insectBoredQuality = insectBoredQuality.get()?.toDouble()
    }


    /**
     * 病斑粒质量
     */
    fun mottledQuality(){
        mottledQuality.set(weighing)
        mRecordCorn?.mottledQuality = mottledQuality.get()?.toDouble()
    }


    /**
     * 破损粒质量
     */
    fun brokenQuality(){
        brokenQuality.set(weighing)
        mRecordCorn?.brokenQuality = brokenQuality.get()?.toDouble()
    }

    /**
     * 生芽粒重量
     */
    fun sproutWeight(){
        sproutWeight.set(weighing)
        mRecordCorn?.sproutWeight = sproutWeight.get()?.toDouble()
    }


    /**
     * 生霉粒重量
     */
    fun moldWeight(){
        moldWeight.set(weighing)
        mRecordCorn?.moldWeight = moldWeight.get()?.toDouble()
    }


    /**
     * 热损伤粒质量
     */
    fun thermalDamageGrainQuality(){
        thermalDamageGrainQuality.set(weighing)
        mRecordCorn?.thermalDamageGrainQuality = thermalDamageGrainQuality.get()?.toDouble()
    }

    /**
     * 霉变粒质量
     */
    fun mildewGrainQuality(){
        mildewGrainQuality.set(weighing)
        mRecordCorn?.mildewGrainQuality = mildewGrainQuality.get()?.toDouble()
    }



    /**
     * 刷新数据
     */
    fun refreshData(){
        //小样重量
        smallMiscellaneousSampleQuality.set(mRecordCorn?.smallMiscellaneousSampleQuality.toString())
        //大样杂质率
        detailResults.set(mRecordCorn?.detailResults.toString())
    }



    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算大样
         */
        var details = 100 - mRecordCorn!!.detailResults
        /**
         * 是否细分
         */
        if(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false)){

            /**
             * 虫蚀率-结果
             */
            var InsectBoredResult = CommonUtils.formatDouble(details * (mRecordCorn?.insectBoredQuality!! / mRecordCorn?.smallMiscellaneousSampleQuality!!))
            mRecordCorn?.insectBoredResult = InsectBoredResult
            insectBoredResult.set(InsectBoredResult.toString())
            /**
             * 病斑率-结果
             */
            var MottledResult = CommonUtils.formatDouble(details * (mRecordCorn?.mottledQuality!! / mRecordCorn?.smallMiscellaneousSampleQuality!!))
            mRecordCorn?.mottledResult = MottledResult
            mottledResult.set(MottledResult.toString())
            /**
             * 破损率-结果
             */
            var BrokenResult = CommonUtils.formatDouble(details * (mRecordCorn?.brokenQuality!! / mRecordCorn?.smallMiscellaneousSampleQuality!!))
            mRecordCorn?.brokenResult = BrokenResult
            brokenResult.set(BrokenResult.toString())
            /**
             * 生芽粒率-结果
             */
            var SproutResult = CommonUtils.formatDouble(details * (mRecordCorn?.sproutWeight!! / mRecordCorn?.smallMiscellaneousSampleQuality!!))
            mRecordCorn?.sproutResult = SproutResult
            sproutResult.set(SproutResult.toString())
            /**
             * 生霉粒率-结果
             */
            var MoldResult = CommonUtils.formatDouble(details * (mRecordCorn?.moldWeight!! / mRecordCorn?.smallMiscellaneousSampleQuality!!))
            mRecordCorn?.moldResult = MoldResult
            moldResult.set(MoldResult.toString())

            /**
             * 热损伤粒质量
             */
            var mField_27 = CommonUtils.formatDouble(details * (mRecordCorn?.thermalDamageGrainQuality!! / mRecordCorn?.smallMiscellaneousSampleQuality!!))
            mRecordCorn?.thermalDamageGrain = mField_27
            thermalDamageGrain.set(mField_27.toString())
            /**
             * 霉变粒结果
             */
            var mMildewGrain = CommonUtils.formatDouble(details * (mRecordCorn?.mildewGrainQuality!! / mRecordCorn?.smallMiscellaneousSampleQuality!!))
            mRecordCorn?.mildewGrain = mMildewGrain
            mildewGrain.set(mMildewGrain.toString())

            /**
             *  不完善粒-结果
             */
            var ResultOfImperfectGrain =  CommonUtils.formatDouble(InsectBoredResult + MottledResult + BrokenResult + SproutResult + MoldResult + mField_27 + mMildewGrain)
            mRecordCorn?.resultOfImperfectGrain = ResultOfImperfectGrain
            resultOfImperfectGrain.set(ResultOfImperfectGrain.toString())

        }else{
            /**
             * 计算小样
             */
            var smallKind = mRecordCorn?.imperfectGrainQuality!! / mRecordCorn?.smallMiscellaneousSampleQuality!!
            /**
             * 霉变粒结果
             */
            var mMildewGrain = CommonUtils.formatDouble(details * (mRecordCorn?.mildewGrainQuality!! / mRecordCorn?.smallMiscellaneousSampleQuality!!))
            mRecordCorn?.mildewGrain = mMildewGrain
            mildewGrain.set(mMildewGrain.toString())

            /**
             * 不完善粒-结果
             */
            var ImperfectGrain = CommonUtils.formatDouble((details * smallKind) + mMildewGrain)
            mRecordCorn?.resultOfImperfectGrain = ImperfectGrain
            resultOfImperfectGrain.set(ImperfectGrain.toString())
        }
        /**
         * 不完善粒记录员90.21
         */
        mRecordCorn!!.imperfectGrainRecorder = mPersonnel!!.name


        //不完善粒质量
        if(mRecordCorn?.imperfectGrainQuality == 0.00){
            imperfectGrainQuality.set("0.00")
        }
        //虫蚀粒质量
        if(mRecordCorn?.insectBoredQuality == 0.00){
            insectBoredQuality.set("0.00")
        }
        //病斑粒质量
        if(mRecordCorn?.mottledQuality == 0.00){
            mottledQuality.set("0.00")
        }
        //破损粒质量
        if(mRecordCorn?.brokenQuality == 0.00){
            brokenQuality.set("0.00")
        }
        //生芽粒重量
        if(mRecordCorn?.sproutWeight == 0.00){
            sproutWeight.set("0.00")
        }
        //生霉粒重量
        if(mRecordCorn?.moldWeight == 0.00){
            moldWeight.set("0.00")
        }
        //热损伤粒质量
        if(mRecordCorn?.thermalDamageGrainQuality == 0.00){
            thermalDamageGrainQuality.set("0.00")
        }
        //霉变粒质量
        if(mRecordCorn?.mildewGrainQuality == 0.00){
            mildewGrainQuality.set("0.00")
        }
    }
}